# CVS: $Id: SAM2ESPS.pm,v 1.1 2007/10/08 09:26:03 schweitz Exp $
# Time-stamp: <11 May 01 16:37:41 giron1>

=head1 NAME

SAM2ESPS - Package for access to SAM language and encoding

=head1 AUTHOR

 $Id: SAM2ESPS.pm,v 1.1 2007/10/08 09:26:03 schweitz Exp $
 Franck Giron

=head1 SYNOPSIS

 use SAM2ESPS;

 $arr_ref = GenSAMExtList($lang,'file_type');

=head1 DESCRIPTION

This package includes some routines for the conversion of SAM
files in ESPS format

=cut 

package SAM2ESPS;

use Exporter ();
use FileHandle;

@ISA = qw(Exporter);
@EXPORT = qw(GenSAMExtList ReadISO2SAM_H);

# Hash Of Files for extension, ISO, and SAM format

%FILE_H = (ITEM  => "$ENV{'HOME'}/perl/dbase/item.SAM",
	   ISO   => "$ENV{'HOME'}/perl/dbase/iso639.SAM",
	   FTYPE => "$ENV{'HOME'}/perl/dbase/ftype.SAM");

#----------------------------------------------------------------------

=head1 FUNCTIONS

Here are the different available functions

=head2 ReadISO2SAM ($langcode,$file)

This subroutine reads the list of iso639, iso639-2 codes, 
languages in english and SAM codes. 

 The following variables have to be defined in the calling routine
 $langcode  : The ISO two letters code of the language desired
 $file      : The name of the file containing a list of ISO codes and SAM
              language extensions

=cut

sub ReadISO2SAM{
  my ($langcode,$file) = @_;
     
  open FILE, "<$file" or die "Can't open $file";

  # Skip all lines until begin of keys description
 LINE: while(<FILE>){last LINE if /^LBD/;}

  # Extract the keys list
  chop;
  my @keylist = split /,\s+|:\s+/;

  # Read all lines until searched ISO language code is found
 SEARCH: while(<FILE>) {
    chop;
    my @values = split /,\s+|:\s+/;
    $found = $values[1] eq $langcode;
    last SEARCH if ($found);
  }
  close FILE;

  my $list = {};
  # Extract the information for each key
  if ($found) {
    my $k;
    for $k (1..$#keylist) {
      $list->{$keylist[$k]} = $values[$k];
    }
    bless $list;
    return($list);
  }
  else {
    # or gives a return message if the language code was not found
    die "code: $langcode not found in $file";
  }
}

=head2 ReadISO2SAM_H ($type)

This subroutine reads the list of iso639, iso639-2 codes,
english language name and SAM code.

 The following variables have to be defined in the calling routine
    $type      : the file type containing the complete list
                 of ISO codes and SAM language extensions

=cut


sub ReadISO2SAM_H{
  my ($type) = @_;

  my $file = $FILE_H{$type};     
  open FILE, "<$file" or die "Can't open $file";

  # Skip all lines until begin of keys description
 LINE: while(<FILE>){last LINE if /^LBD/;}

  # Extract the keys list
  chop;
  my @keylist = split /,\s+|:\s+/;

  # Read all lines of the array
  my $list = {};
  while (<FILE>) {
    chop;
    my @values = split /,\s+|:\s+/;
    if ($values[0] eq 'LBC') {
      for $k (2..$#keylist) {
	$list->{$values[1]}->{$keylist[$k]} = $values[$k];
      }
    }
  }
  close FILE;

  bless $list,$type;
  return $list;
}

=head2 ReadH ($type)

This subroutine returns a hash from a file defined within SAM format

The following variable has to be defined in the calling routine
    $type : the type of the file containing the list of items and 
            SAM extensions

=cut

sub ReadH{ 
  my ($type) = @_;

  my $file = $FILE_H{$type};
  open FILE, "$file" or die "Can't open $file";

  # Skip all lines until the description of keys
 LINE: while(<FILE>){last LINE if /^LBD/;}

  # Read the hash until list end
  my $list = {};
  while (<FILE>) {
    chop;
    my ($code,$key,$value) = split /,\s+|:\s+/;
    if ($code eq 'LBC') {
      $list->{$key} = $value;
    }
  }
  close(FILE);
  
  bless $list,$type;
  return $list;
}

=head2 GenSAMExtList ($lang,$ftype)

This subroutine returns an array of SAM extensions

The following variables have to be defined in the calling routine
  $lang   : the ISO639.2 language code
  $ftype  : the file type expected

=cut

sub GenSAMExtList{ 
  my ($lang,$ftype) = @_; 

  # Hash of SAM items
  #    my $item_h = ReadH('ITEM');
  #    foreach (keys %{$item_h}) {
  #	$ext = $item_h->{$_} . $iso_h->{$lang}{SAM} . $ftype_h->{$ftype};
  #	push @$array, $ext;
  #    }

  # Hash of SAM file types
  my $ftype_h = ReadH('FTYPE');

  # ISO 639 and SAM language codes file
  my $iso_h = ReadISO2SAM_H('ISO');

  # Add the \w option to search for any type of corpuses

  my $array = []; 
  my $ext = '\w' .  $iso_h->{$lang}{SAM} . $ftype_h->{$ftype};
  push @$array, $ext;

  bless $array;
  return($array);
}
1;


